package com.yao.seckill.core.bean;


import lombok.*;

/**
 * Copyright (C), 2022-2022, 姚兵
 * Author: 32210
 * Date: 2022/8/30 20:00
 * FileName: SeckillResponse
 * Description: response返回的公共bean
 */


@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class SeckillResponse<T> {

    private Integer code;
    private String msg;
    private T data;

    public SeckillResponse(Integer code,String msg){
        this.code=code;
        this.msg=msg;
    }
    //默认成功返回
    private static final SeckillResponse defaultSuccess=new SeckillResponse(ResponseEnum.SUCCESS.code, ResponseEnum.SUCCESS.msg);

    //默认失败返回
    private static final SeckillResponse defaultError=new SeckillResponse(ResponseEnum.INTERNAL_ERROR.code, ResponseEnum.INTERNAL_ERROR.msg);

    public  enum  ResponseEnum{

        SUCCESS(200,"SUCCESS"),
        INTERNAL_ERROR(500,"INTERNAL_ERROR");
        Integer code;
        String msg;
        ResponseEnum(Integer code,String msg){
            this.code=code;
            this.msg=msg;
        }
    }

    public static SeckillResponse success(){
        return defaultSuccess;
    }


    public static <T> SeckillResponse  success(T data){
        return new SeckillResponse<T>(ResponseEnum.SUCCESS.code,ResponseEnum.SUCCESS.msg,data);
    }


    public static SeckillResponse error(){
        return defaultError;
    }

    public static SeckillResponse error(Integer code ,String msg){
        return new SeckillResponse(code,msg);
    }

    public static SeckillResponse error(ResponseEnum responseEnum){
        return new SeckillResponse(responseEnum.code, responseEnum.msg);
    }

    public static <T> SeckillResponse error(String msg,T data){
        return new SeckillResponse<T>(ResponseEnum.INTERNAL_ERROR.code, msg,data);
    }

    public  static <T>  SeckillResponse error(Integer code ,String msg,T data){
        return new SeckillResponse<T>(code,msg,data);
    }
}
